﻿/// <reference group="Dedicated Worker" />
onmessage = function (event) {
    //document.getElementById("log").textContent = event.data;
}
start();



var streamWebSocket;
var dataWriter;
var dataReader;
var data = "";
var countOfDataSent;
var countOfDataReceived;


function start() {
    if (streamWebSocket) {
        log("Already started.");
        return;
    }
    log("Starting");

    var webSocket = new Windows.Networking.Sockets.StreamWebSocket();
    webSocket.onclosed = onClosed;

    var uri = new Windows.Foundation.Uri("ws://localhost:8051/client/");

    log("Connecting to: " + uri.absoluteUri);

    webSocket.connectAsync(uri).then(function () {
        log("Connected");
        streamWebSocket = webSocket;
        dataWriter = new Windows.Storage.Streams.DataWriter(webSocket.outputStream);
        dataReader = new Windows.Storage.Streams.DataReader(webSocket.inputStream);
        // When buffering, return as soon as any data is available.
        dataReader.inputStreamOptions = Windows.Storage.Streams.InputStreamOptions.partial;
        countOfDataSent = 0;
        countOfDataReceived = 0;

        //  send data to the server
        Connect();

        readIncoming();

    }, function (e) {
        log("Failed to connect: " + getError(e));
    });
}

function Connect() {
    try {
        var size = dataWriter.measureString(data);
        countOfDataSent += size;
        dataWriter.writeString(data);
        dataWriter.storeAsync();
    }
    catch (e) {
        log("Sync write error: " + getError(e));
    }
}

function writeError(e) {
    log("Write error: " + getError(e));
}

function readIncoming(args) {

    // Buffer as much data as you require for your protocol.
    dataReader.loadAsync(100).then(function () {
        countOfDataReceived += dataReader.unconsumedBufferLength;
        //document.getElementById("data").textContent += "\n"+countOfDataReceived;

        
        var incomingBytes = dataReader.readString(dataReader.unconsumedBufferLength);

        postMessage(incomingBytes);
        // Do something with the data.
        // Alternatively you can use DataReader to read out individual booleans,
        // ints, strings, etc.  Be sure to check that UnconsumedBufferLength has
        // enough data for you first.

        readIncoming(); // Start another read
    }, readError);
}

function readError(e) {
    log("Read Error: " + getError(e));
}

function onClosed(args) {
    log("Closed; Code: " + args.code + " Reason: " + args.reason);
    if (streamWebSocket) {
        streamWebSocket.close();
    }
    streamWebSocket = null;
}

function closeSocket() {
    if (!streamWebSocket) {
        log("Not connected.");
        return;
    }

    log("Closing");
    streamWebSocket.close(1000, "Closed due to user request.");
    streamWebSocket = null;
}

function log(text) {
    //document.getElementById("log").textContent = text;
}